//
//  CollectionViewController.m
//  项目A_百思不得姐
//
//  Created by wudongyang on 16/6/4.
//  Copyright © 2016年 wudongyang. All rights reserved.
//

#import "CollectionViewController.h"
#import "DataFMDBHandle.h"
#import "MineCollectModel.h"
#import "CommentsViewController.h"
@interface CollectionViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)UITableView *myTableView;
@property(nonatomic, strong)NSMutableArray *myArray;
@end

@implementation CollectionViewController
-(void)viewWillAppear:(BOOL)animated{
    NSUserDefaults *defa = [NSUserDefaults standardUserDefaults];
    NSString *userName = [defa objectForKey:@"userName"];
    self.myArray = [NSMutableArray arrayWithArray:[[DataFMDBHandle shareDatHandle] selectAllCollecData:userName withCompare:YES]];
    [self.myTableView reloadData];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的收藏";
    self.view.backgroundColor = [UIColor whiteColor];
    self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.myTableView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xE8E8E8,0x343434);
    [self.view addSubview:self.myTableView];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    // Do any additional setup after loading the view.
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    MineCollectModel *model = self.myArray[indexPath.row];
    [[DataFMDBHandle shareDatHandle] deleteCollectData:model.title];
    [self.myArray removeObjectAtIndex:indexPath.row];
    [self.myTableView reloadData];

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.myArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    MineCollectModel *model = [self.myArray objectAtIndex:indexPath.row];
    cell.textLabel.text = model.title;
    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xE8E8E8,0x343434);
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MineCollectModel *model = self.myArray[indexPath.row];
    CommentsViewController *commentsVC = [[CommentsViewController alloc] init];
    commentsVC.myID = model.collectId;
    commentsVC.myText = model.title;
    commentsVC.myGif = model.Gif;
    commentsVC.myImage = model.Image;
    commentsVC.myVideo = model.Video;
    commentsVC.myType = model.Type;
    commentsVC.myUrl = model.Url;
    
    [self.navigationController pushViewController:commentsVC animated:YES];


}














@end
